import libtcodpy as libtcod
import ent, items, events

#shit like windows or doors, or tables		
#-----------------------------------------------------------------------
class Furniture(ent.Entity):
	def __init__(self, em, x, y, char, color):
		super(Furniture,self).__init__(em, x, y, char, color)
		
		self.name  = "Furniture"
		self.locked = False
		self.obstacle = True
		
	def die(self):
		super(Furniture,self).die()
		
		self.em.services["render"].message(self.name+" is broken");
		self.model = "X"
		
		self.em.services["map"].get_tile(self.x,self.y).block_sight = False
		self.em.services["map"].get_tile(self.x,self.y).blocked 	 = False
		self.obstacle = False		
		self.locked = False
		
		self.em.services["map"].update_fov_map()
		
		
	def describe(self, render):
		if self.hp < 0:
			render.message(self.name + " is broken");
			
	def get_action_list(self):
		
		class OpenAction(ent.EntAction):
			def act(self, caller):
				self.owner.em.post(events.PlayerOpenContainer(self.owner))
	
		a_list = []
		if self.container:
			a_list.append(OpenAction(self,"Open"))


		return a_list

#-----------------------------------------------------------------------

#-----------------------------------------------------------------------				
				
class Door(Furniture):
	def is_obstacle(self):
		if self.locked:
			return true
			
	def unlock(self):
		self.locked = False
		self.model = "/"
		self.tile.block_sight = False
		self.obstacle = False
		
		#self.em.services["map"].tiles[self.x][self.y].obstacle = False
		#self.em.services["map"].tiles[self.x][self.y].bloacked = False
		
		self.em.services["map"].update_fov_map()
		
	def lock(self):
		self.locked = True
		self.model = "+"
		self.tile.block_sight = True
		self.obstacle = True
		
		self.em.services["map"].update_fov_map()
				
	def get_action_list(self):
		
		class LockAction(ent.EntAction):
			def act(self, caller):
				self.owner.lock()
						
		class UnlockAction(ent.EntAction):
			def act(self, caller):
				self.owner.unlock()

		a_list = []
		if self.locked:
			a_list.append(UnlockAction(self,"Unlock"))
		else:
			a_list.append(LockAction(self,"Lock"))

		return a_list
				
#-----------------------------------------------------------------------
#	Refrigirator is a storage furniture with lots of crappy food

class Refrigirator(Furniture):
	def __init__(self, em, x, y, color):
		super(Furniture,self).__init__(em, x, y, "R", color)
		self.name = "Refrigirator"
		self.set_container(ent.BaseContainer())
		self.obstacle = True
		self.hp = 100
		
		#TODO: introduce capacity for a container
		
		food_count = libtcod.random_get_int(0, 0, 5)
		for i in xrange(food_count):
			self.add_food()
			
	#add random food unit
	def add_food(self):
		#todo: make a random food title there
		food = items.FoodItem()
		food.pick_up(self)
		
		
class Sink(Furniture):
	def get_action_list(self):
		
		class WashAction(ent.EntAction):
			def act(self, caller):
				#caller = self.owner.em.services["player"]
				
				caller.color = libtcod.white
				if caller.bloody:
					self.owner.em.post(events.ConsoleMsg("You washed the blood off your hands"))
				else:
					self.owner.em.post(events.ConsoleMsg("You washed your hands"))
					
				caller.bloody = False

		a_list = []
		a_list.append(WashAction(self,"Wash"))

		return a_list
		

